<template>
  <div class="Home_container">
    <el-container class="Home_container">
      <!-- 头部区域 -->
      <el-header>
        <div class="log">
          <h1>后台管理系统</h1>
        </div>
        <el-button
          class="LoginOut_btn"
          type="info"
          @click="login_out"
        >退出</el-button>
      </el-header>
      <el-container>
        <!-- 左侧边栏区域 -->
        <el-aside :width="isCollapse ? '64px':'200px'">
          <div
            class="toggle-button"
            @click="toggleCollapse"
          >|||</div>
          <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="skyblue"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition='false'
            router
          >
            <!-- 一级菜单 -->
            <!-- 因为index相同所以动态绑定一个:index，但它的值必须是字符串所以用String()函数装换成字符串 -->
            <el-submenu
              :index="String(item.id)"
              v-for="item in menuList"
              :key="item.id"
            >
              <!-- 一级菜单的模板 -->
              <template slot="title">
                <!-- 一级菜单的图标 -->
                <!-- 这里真的妙啊，原来通过动态绑定属性可以做到这么花哨的功能 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 一级菜单的名称 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/'+subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="seveNavStatus('/'+subItem.path)"
              >
                <!-- 二级菜单的模板 -->
                <template slot="title">
                  <!-- 二级菜单的图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 二级菜单的名称 -->
                  <span>{{item.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>

</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        101: 'iconfont icon-user',
        102: 'iconfont icon-tijikongjian',
        103: 'iconfont icon-shangpin',
        125: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的地址
      activePath: ''
    }
  },
  // 生命周期
  // 在程序开始的时候调用这个函数
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出按钮
    login_out () {
      // 清空tooken
      window.sessionStorage.clear()
      // 重新定向到login
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      // 调用axios发起请求
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        // this.$message.success(res.meta.msg)
        this.menuList = res.data
        // console.log(this.menuList)
      }
    },
    // 点击按钮伸缩侧边菜单栏
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存点击时的index,一会赋给default-active
    seveNavStatus (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.Home_container {
  height: 100%;
}
.el-container {
  background-color: #373d41;
}
.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
}
.el-aside {
  background-color: #333744;
  // 美化边框
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  // 鼠标滑过改变样式
  cursor: pointer;
}
</style>
